package com.imy.luascript.app.ui.code;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.ahmadaghazadeh.editor.widget.CodeEditor;
import com.imy.common.util.ToastUtil;
import com.imy.lua.Constant;
import com.imy.lua.LuaCore;
import com.imy.luascript.R;
import com.imy.luascript.base.activity.BaseViewActivity;
import com.imy.common.base.adapter.BaseDelegate;
import com.imy.luascript.utils.AssetsUtils;
import com.imy.luascript.viewmodel.BaseViewModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * create by Imy
 */
public class CodeActivity extends BaseViewActivity {
    @BindView(R.id.editor)
    CodeEditor mEditor;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.run_btn)
    LinearLayout runBtn;
    @BindView(R.id.save_btn)
    LinearLayout saveBtn;

    public static void start(Context context, String path, String title) {
        Intent intent = new Intent(context, CodeActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @OnClick(R.id.save_btn)
    public void  onSaveBtn(){
        ToastUtil.toast(self,getResources().getString(R.string.un_ok));
    }

    @OnClick(R.id.run_btn)
    public void onRunClicked() {
        String path = Constant.ASSETS_PREFIX+ getPath() + "/main.lua";
        LuaCore.get().start(self,path);
    }

    private String getPath() {
        return getIntent().getStringExtra("path");

    }

    @Override
    protected void initView() {
        setToolbarTitle(getIntent().getStringExtra("title") + "示例");
        String path = getPath();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        try {

            String[] list = getResources().getAssets().list(path);
            List<String> arrayList =new ArrayList<>();
            for (int i = 0; i < list.length; i++) {
                String s = list[i];
                if (s.endsWith(".lua")){
                    arrayList.add(s);
                }
            }
            CodeFileListAdapter adapter = new CodeFileListAdapter(this, arrayList);
            adapter.getCodeFileDelegate().setCurrentFile("main.lua");
            //首次加载main
            adapter.getCodeFileDelegate().setOnItemClickListener(new BaseDelegate.ItemClickListener<String>() {
                @Override
                public void onItemClicked(int position, String o, View view) {
                    adapter.getCodeFileDelegate().setCurrentFile(o);
                    setText(path + "/" + o);
                    adapter.notifyDataSetChanged();
                }
            });
            setText(path + "/main.lua");
            mRecyclerView.setAdapter(adapter);
            adapter.hideLoadMore();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void setText(String s) {
        String string = AssetsUtils.readAssets(this, s);
        mEditor.setText(string, 1);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_code;
    }

}
